package com.lecture.springlecture.controller;

import com.lecture.springlecture.dao.StudentMapper;
import com.lecture.springlecture.entity.Score;
import com.lecture.springlecture.entity.UserEntity;
import com.lecture.springlecture.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private UserService userService;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CacheManager cacheManager;

    /**
     *  查找所有
     * @return
     */
    @RequestMapping("/getAll")
    public List<UserEntity> getAll(){
        logger.info("执行getAll方法");
        return userService.getAll();
    }
    /**
     * 根据id获取用户
     * @return
     */
    @RequestMapping("/getOne")
    public UserEntity getOne(Long id){
        return userService.getOne(id);
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping("/insertUser")
    public String insertUser(UserEntity user) {
        userService.insertUser(user);
        return "insert success";
    }
    /**
     * 修改用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(UserEntity user) {
        userService.updateUser(user);
        return "update success";
    }

    @GetMapping("cacheTest")
    public void cacheTest() {
        Cache user = cacheManager.getCache("user");
        Object o = cacheManager.getCache("user").get("").get();
        System.out.println(o);
    }

    @GetMapping("updateScore")
    public void updateScore() {
        List<Score> scoreList = new ArrayList<>();
        Score s1 = new Score();
        s1.setId(1);
        s1.setName("张三");
        s1.setScore(100);
        s1.setSubject("数学");

        Score s2 = new Score();
        s2.setId(2);
        s2.setName("张三");
        s2.setScore(100);
        s2.setSubject("数学");

        scoreList.add(s2);
        scoreList.add(s1);

        studentMapper.updateScore(scoreList);
    }
}
